package com.zqxq.lottery.cms.controller;

import com.zqxq.common.constant.Result;
import com.zqxq.lottery.cms.feign.LotteryServerFeignClient;
import com.zqxq.lottery.entity.bo.product.ProductBO;
import com.zqxq.lottery.entity.bo.product.ProductSearchBO;
import com.zqxq.lottery.entity.vo.product.ProductVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.HttpException;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Api(value = "商品管理",tags = "商品管理")
@RestController
@Slf4j
@RequestMapping(value = "/product", produces = "application/json")
public class ProductController {

    @Autowired
    private LotteryServerFeignClient lotteryServerFeignClient;
    @ApiOperation(value = "商品-保存", notes="商品-保存")
    @PostMapping("/save")
    public Result saveProduct(ProductVO vo){
            return lotteryServerFeignClient.saveProduct(vo);
    }

    @ApiOperation(value = "商品-查询", notes="商品-查询")
    @PostMapping("/page")
    public Result queryProductPage(ProductBO search){
        return lotteryServerFeignClient.queryProductPage(search);
    }

    @ApiOperation(value = "商品-excel导入（批量新增）", notes="商品-excel导入（批量新增）")
    @PostMapping("/excelBatchInsert")
    public Result excelBatchInsert(MultipartFile file){
        Map<String, File> fileMap = new HashMap<>();
        String fileName = file.getOriginalFilename();
        File files = null;
        try {
            files=File.createTempFile("tmp", null);
            file.transferTo(files);
            files.deleteOnExit();
        } catch (HttpException e) {
            e.printStackTrace();
            log.error("fileUpload HttpException",e);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("IOException HttpException",e);
        }
        fileMap.put("image_ref1",files);
        return lotteryServerFeignClient.excelBatchInsert(fileName,fileMap,false);
    }
    @ApiOperation(value = "商品-excel导入（批量修改）", notes="商品-excel导入（批量修改）")
    @PostMapping("/excelBatchUpdate")
    public Result excelBatchUpdate(MultipartFile file){
        Map<String, File> fileMap = new HashMap<>();
        String fileName = file.getOriginalFilename();
        File files = null;
        try {
            files=File.createTempFile("tmp", null);
            file.transferTo(files);
            files.deleteOnExit();
        } catch (HttpException e) {
            e.printStackTrace();
            log.error("fileUpload HttpException",e);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("IOException HttpException",e);
        }
        fileMap.put("image_ref1",files);
        return lotteryServerFeignClient.excelBatchUpdate(fileName,fileMap,true);
    }
    @ApiOperation(value = "商品-excel导出", notes="商品-excel导出")
    @PostMapping("/excelBatchExport")
    public void excelBatchExport(ProductSearchBO search, HttpServletResponse response){
        Result<List<ProductVO>> result = lotteryServerFeignClient.excelBatchExport(search);
        List<ProductVO> list = result.getData();
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("商品信息表");
        String fileName = "productInfo"  + ".xls";//设置要导出的文件的名字
        //新增数据行，并且设置单元格数据
        int rowNum = 1;
        String[] headers = {"产品编号", "产品种类", "产品类型", "产品", "型号", "颜色", "版本", "原价（元）", "兑换量（金豆）", "排序"};
        //headers表示excel表中第一行的表头
        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头
        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        //在表中存放查询到的数据放入对应的列
        for (ProductVO product : list) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(product.getId());
            String productCalss = product.getCalss()==0?"实体商品":"数字商品";
            row1.createCell(1).setCellValue(productCalss);
            row1.createCell(2).setCellValue(product.getType());
            row1.createCell(3).setCellValue(product.getDetailType());
            row1.createCell(4).setCellValue(product.getModel());
            row1.createCell(5).setCellValue(product.getColor());
            row1.createCell(6).setCellValue(product.getVersion());
            row1.createCell(7).setCellValue(product.getPrice());
            row1.createCell(8).setCellValue(product.getExchangeRate());
            row1.createCell(9).setCellValue(product.getSort());
            rowNum++;
        }
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        try {
            response.flushBuffer();
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
